package com.pikaqiu.warehouse.exception;

import com.pikaqiu.warehouse.domain.common.MiniAjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 *
 * @author ruoyi
 */
@Order(1)
@RestControllerAdvice(basePackages = "com.pikaqiu.warehouse")
@Slf4j
public class MiniGlobalExceptionHandler {

    /**
     * 业务异常
     */
    @ExceptionHandler(MiniServiceException.class)
    public MiniAjaxResult<Integer> handleMiniServiceException(MiniServiceException e, HttpServletRequest request) {
        return MiniAjaxResult.error(e.getMessage());
    }

    /**
     * 小程序登陆异常
     */
    @ExceptionHandler(MiniAuthException.class)
    public MiniAjaxResult<Integer> handleMiniAuthException(MiniAuthException e) {
        return MiniAjaxResult.error(MiniAjaxResult.Type.MINI_AUTH, e.getMessage());
    }

    @ExceptionHandler(MiniCompanyAuthException.class)
    public MiniAjaxResult<Integer> handleMiniCompanyAuthException(MiniCompanyAuthException e) {
        return MiniAjaxResult.error(MiniAjaxResult.Type.NO_COMPANY, e.getMessage());
    }

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public MiniAjaxResult<Integer> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
        return MiniAjaxResult.error("参数异常");
    }

    /**
     * 小程序登陆异常
     */
    @ExceptionHandler(Exception.class)
    public MiniAjaxResult<Integer> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return MiniAjaxResult.error("系统错误，请联系管理员");
    }
}
